#####################################################
### PAPER: THE ITALIAN POLITICAL CLASS
### FILE NAME: "9 - To create all graphs.R"
### AUTHOR: Isernia, Martini, Olmastroni, Verzichelli
#####################################################


# LOAD /INSTALL PACKAGES
pacman::p_load(tidyverse, magrittr, haven, labelled, 
               lubridate, stringr, readxl, ggpubr, 
               lattice,foreign,RColorBrewer,reshape2, 
               xtable, gdata, zoo, plotly, psych, 
               latticeExtra, gridExtra, boot, showtext, scales, 
               ggsci, wesanderson, tibble)


# SET WORKING DIRECTORY
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))



# FIGURE 1 DATA
figure1 <- read_excel("figure1.xlsx")

# FIGURE 1 ORDER
figure1$variable  <- factor(figure1$variable, 
                            levels=c("Year entered parliament",
                                     "N of elections won",
                                     "Political experience (parliament)",
                                     "Political experience (municipality)",
                                     "Role in commissions",
                                     "In government/In opposition",
                                     "Political coalition",
                                     "Political group",
                                     "Party/list of election",
                                     "Political position",
                                     "Date of election",
                                     "Population size (municipality)",
                                     "Job sector",
                                     "Area (election)",
                                     "Type of degree",
                                     "Education",
                                     "Age classes",
                                     "Gender"))

figure1$level  <- factor(figure1$level, levels=c("municipal", "regional", "national/EU"))

figure1 %>% 
  ggplot(aes(x = level, y = variable)) + 
  geom_point(size = 3, pch = 0) + 
  geom_point(data =  dplyr::filter(figure1, information == 1), 
             size = 3,
             aes(x = level, y = variable),
             pch = 22, fill = "black") +
  theme_bw() +
  ylab("") + xlab("") +
  theme(
    axis.text=element_text(size=12),
    axis.ticks.y = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(c(.15, .15, 0, .15), "cm"),
    legend.text = element_text(size = 7),
    legend.title = element_blank(), 
    legend.key.size = unit(.8, "line"),
    legend.position = c(.2, .85),
    legend.background = element_rect(fill = "transparent", 
                                     size = 0.5, 
                                     linetype = "solid"))

ggsave("figure1.png", width = 7, height = 5, dpi = 300)


# FIGURE 2 DATA MUNICIPALITY
figure2 <- read_dta("field_municipal.dta")
figure2$datestart2 <- ymd(figure2$datestart)

# FIGURE 2 MUNICIPALITY GRAPH
municipality <- ggplot(data=figure2) +
  facet_wrap(~level, nrow = 1L) +
  geom_histogram(aes(x=datestart2,fill = completed), colour = 2, fill = "white",
                 bins = 115) +
  theme_bw() +
  ylab("") + xlab("") +
  theme(panel.grid = element_blank()) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank()) +
  geom_line(aes(x=datestart2, y= cumulative_ncompleted, linetype = "cumulative_ncompleted"), lwd = 0.5) +
  geom_line(aes(x=datestart2, y= cumulative_npartial, linetype = "cumulative_npartial"), lwd = 0.5) +
  scale_linetype_manual(values = c("cumulative_ncompleted" = 1, "cumulative_npartial" = 2), 
                        labels=c('Completed interviews', 'Partial interviews')) +
  scale_x_date(limits = as.Date(c("2020-09-30", "2021-01-31")),
               date_breaks = "1 month", date_minor_breaks = "1 week",
               date_labels = "%Y-%m") +
  scale_y_continuous(limits=c(0, 2100), breaks=seq(0, 2000, by=200)) +
  theme(plot.margin = unit(c(.80, .80, .15, .15), "cm")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-20")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-20"), size = 3,
           y = 20, angle = 90, label = "Field start",
           color = "gray50"[1], hjust = -4.95, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-26")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-26"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -5.0, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-13")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-11-13"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -5.0, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-12")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-12"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -5.0, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-04")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-11-04"), size = 3,
           y = 20, angle = 90, label = "Phone call start",
           color = "gray50"[1], hjust = -2.8, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-17")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-17"), size = 3,
           y = 20, angle = 90, label = "Phone call end",
           color = "gray50"[1], hjust = -2.95, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2021-01-30")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2021-01-30"), size = 3,
           y = 20, angle = 90, label = "Field end",
           color = "gray50"[1], hjust = -5.4, vjust = -0.5)

ggsave("figure2_city.png", width = 7, height = 5, dpi = 300)


# FIGURE 2 DATA REGION
figure2 <- read_dta("field_regional.dta")
figure2$datestart2 <- ymd(figure2$datestart)

# FIGURE 2 REGION GRAPH
region <- ggplot(data=figure2) +
  facet_wrap(~level, nrow = 1L) +
  geom_histogram(aes(x=datestart2,fill = completed), colour = 2, fill = "white",
                 bins = 115) +
  theme_bw() +
  ylab("") + xlab("") +
  theme(panel.grid = element_blank()) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank()) +
  geom_line(aes(x=datestart2, y= cumulative_ncompleted, linetype = "cumulative_ncompleted"), lwd = 0.5) +
  geom_line(aes(x=datestart2, y= cumulative_npartial, linetype = "cumulative_npartial"), lwd = 0.5) +
  scale_linetype_manual(values = c("cumulative_ncompleted" = 1, "cumulative_npartial" = 2), 
                        labels=c('Completed interviews', 'Partial interviews')) +
  scale_x_date(limits = as.Date(c("2020-09-30", "2021-01-31")),
               date_breaks = "1 month", date_minor_breaks = "1 week",
               date_labels = "%Y-%m") +
  scale_y_continuous(limits=c(0, 130), breaks=seq(0, 120, by=20)) +
  theme(plot.margin = unit(c(.80, .80, .15, .15), "cm")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-09-30")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-09-30"), size = 3,
           y = 20, angle = 90, label = "Field start",
           color = "gray50"[1], hjust = -4.1, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-08")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-08"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-16")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-16"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-31")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-31"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-11")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-11"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-21")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-21"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-04")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-11-04"), size = 3,
           y = 20, angle = 90, label = "Phone call start",
           color = "gray50"[1], hjust = -2.25, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-17")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-17"), size = 3,
           y = 20, angle = 90, label = "Phone call end",
           color = "gray50"[1], hjust = -2.35, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2021-01-30")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2021-01-30"), size = 3,
           y = 20, angle = 90, label = "Field end",
           color = "gray50"[1], hjust = -4.4, vjust = -0.5)

ggsave("figure2_region.png", width = 7, height = 5, dpi = 300)


# FIGURE 2 DATA NATION/EUROPE
figure2 <- read_dta("field_nationaleu.dta")
figure2$datestart2 <- ymd(figure2$datestart)

# FIGURE 2 NATION/EUROPE GRAPH
nationaleu <- ggplot(data=figure2) +
  facet_wrap(~level, nrow = 1L) +
  geom_histogram(aes(x=datestart2,fill = completed), colour = 2, fill = "white",
                 bins = 115) +
  theme_bw() +
  ylab("") + xlab("") +
  theme(panel.grid = element_blank()) +
  theme(legend.position = "bottom") +
  theme(legend.title=element_blank()) +
  geom_line(aes(x=datestart2, y= cumulative_ncompleted, linetype = "cumulative_ncompleted"), lwd = 0.5) +
  geom_line(aes(x=datestart2, y= cumulative_npartial, linetype = "cumulative_npartial"), lwd = 0.5) +
  scale_linetype_manual(values = c("cumulative_ncompleted" = 1, "cumulative_npartial" = 2), 
                        labels=c('Completed interviews', 'Partial interviews')) +
  scale_x_date(limits = as.Date(c("2020-09-30", "2021-01-31")),
               date_breaks = "1 month", date_minor_breaks = "1 week",
               date_labels = "%Y-%m") +
  scale_y_continuous(limits=c(0, 130), breaks=seq(0, 120, by=20)) +
  theme(plot.margin = unit(c(.80, .80, .15, .15), "cm")) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-05")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-05"), size = 3,
           y = 20, angle = 90, label = "Field start",
           color = "gray50"[1], hjust = -4.1, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-13")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-13"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-19")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-19"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-10-31")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-10-31"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-22")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-11-22"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-08")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-08"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-21")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-21"), size = 3,
           y = 20, angle = 90, label = "Reminder",
           color = "gray50"[1], hjust = -4.15, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-11-04")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-11-04"), size = 3,
           y = 20, angle = 90, label = "Phone call start",
           color = "gray50"[1], hjust = -2.25, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2020-12-17")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2020-12-17"), size = 3,
           y = 20, angle = 90, label = "Phone call end",
           color = "gray50"[1], hjust = -2.35, vjust = -0.5) +
  geom_vline(xintercept = as.numeric(as.Date("2021-01-30")), linetype = 3, 
             color = "gray50"[1],
             show.legend = T) +
  annotate(geom = "text", x = as.Date("2021-01-30"), size = 3,
           y = 20, angle = 90, label = "Field end",
           color = "gray50"[1], hjust = -4.4, vjust = -0.5)

ggsave("figure2_nationaleu.png", width = 7, height = 5, dpi = 300)


# FIGURE 3 DATA
figure3 <- read_excel("figure3.xlsx")
figure3$estimate <- as.numeric(as.character(figure3$estimate))
figure3$level  <- factor(figure3$level, levels=c("municipal", "regional", "national/EU"))

# FIGURE 3 ORDER
figure3$variable  <- factor(figure3$variable, 
                            levels=c("Non-partisan list", "M5S",
                                     "PD", "League",
                                     "FI", "FdI",
                                     "Tertiary", "Non-tertiary",
                                     "Abroad", "South-Islands",
                                     "Centre", "North-East",
                                     "North-West", ">54",
                                     "35-54", "18-34",
                                     "Males", "Females"))

# FIGURE 3 GRAPH
ggplot(data=figure3,
       aes(x = estimate, color = sample, y = variable,)) +
  facet_wrap(~level, nrow = 1L) +
  geom_point(size = 3, position = position_dodge(0.9),
             pch = 1) +
  ylab("") + xlab("") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  theme(legend.position = "bottom") +
  theme(axis.text=element_text(size=12)) +
  theme(legend.title=element_blank()) +
  guides(fill=guide_legend(nrow=1,byrow=TRUE)) +
  scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, by=20)) + 
  scale_y_discrete(limits = c("Non-partisan list", "M5S", "PD", "League", "FI", "FdI", "Party/List",
                              "Tertiary", "Non-tertiary", "Education",
                              "South-Islands", "Centre", "North-East", "North-West", "Area",
                              ">54", "35-54", "18-34", "Age")) +
  geom_hline(mapping=aes(yintercept=variable), 
             lwd=0.2, linetype = "dashed", colour="grey10") +
  theme(axis.text.y = element_text(face=c("plain", "plain", "plain", "plain", "plain", "plain",
                                          "bold",
                                          "plain", "plain", "bold",
                                          "plain", "plain", "plain", "plain",
                                          "bold", 
                                          "plain", "plain", "plain", 
                                          "bold",
                                          "plain", "plain", 
                                          "bold")))

ggsave("figure3.png", width = 7, height = 8, dpi = 300)



# FIGURE 4 DATA
figure4 <- read_excel("figure4.xlsx")

# FIGURE 4 ORDER
figure4$variable  <- factor(figure4$variable, 
                            levels=c("Socio-demographic features",
                                     "Economy",
                                     "Immigration",
                                     "European integration",
                                     "COVID-19",
                                     "RELEVANT ISSUES",
                                     "Issue congruence (public opinion party/list of election)",
                                     "Left-right placement (self+parties)",
                                     "Factors that matter for political decision",
                                     "Role and nature of public opinion", 
                                     "Experiment: public opinion influence in decisions",
                                     "Attachment to different polities",
                                     "Importance of different political offices",
                                     "Opinions on the reduction of Parlamentarians",
                                     "Represent interests: citizens vs interest groups",
                                     "Level of representation: collective vs individual representation",
                                     "Role of elections: retrospective vs prospective",
                                     "Role of the representative: main aspects and relative importance",
                                     "Experiment: legitimacy of different decision-making procedures",
                                     "Satisfaction with democracy",
                                     "Populist attitudes",
                                     "Opinion on different political systems",
                                     "Problems of democratic representation",
                                     "Experiment: features of the good politician",
                                     "Opinions on declining politicians' reputation"))

# FIGURE 4 GRAPH
ggdotchart(figure4, x = "variable", y = "estimate",
           color = "group", sorting = "descending",  
           add = "segments", rotate = TRUE,
           group = "group", dot.size = 7,
           label = round(figure4$estimate), font.label = list(color = "white", size = 9, 
           vjust = 0.5), ggtheme = theme_bw()) +
  ylab("") + xlab("") +
  scale_x_discrete(limits = c("Socio-demographic features",
                              "Profile",
                              "European integration",
                              "Economy",
                              "Immigration",
                              "COVID-19",
                              "Relevant issues",
                              "Issue congruence (public opinion party/list of election)",
                              "Left-right placement (self+parties)",
                              "Perceived congruence",
                              "Factors that matter for political decision",
                              "Experiment: public opinion influence in decisions",
                              "Role and nature of public opinion", 
                              "Propensity for responsiveness",
                              "Opinions on the reduction of Parlamentarians",
                              "Importance of different political offices",
                              "Attachment to different polities",
                              "Locus of representation",
                              "Represent interests: citizens vs interest groups",
                              "Level of representation: collective vs individual representation",
                              "Role of elections: retrospective vs prospective",
                              "Role of the representative: main aspects and relative importance",
                              "Focus of representation",
                              "Satisfaction with democracy",
                              "Opinions on declining politicians' reputation",
                              "Experiment: legitimacy of different decision-making procedures",
                              "Experiment: features of the good politician",
                              "Problems of democratic representation",
                              "Opinion on different political systems",
                              "Populist attitudes",
                              "Democratic challenges")) +
  scale_y_continuous(limits=c(0, 12), breaks=seq(0, 12, by=1)) + 
  theme(panel.grid = element_blank()) +
  theme(legend.position = "non") +
  theme(axis.text=element_text(size=10)) +
  theme(axis.text.y = element_text(face=c("plain",
                                          "bold",
                                          "plain", "plain", "plain", "plain",
                                          "bold",
                                          "plain", "plain", 
                                          "bold",
                                          "plain", "plain", "plain",
                                          "bold",
                                          "plain", "plain", "plain",
                                          "bold", 
                                          "plain", "plain", "plain", "plain",
                                          "bold", 
                                          "plain", "plain", "plain", "plain", "plain", "plain", "plain",
                                          "bold")))

ggsave("figure4.png", width = 7, height = 9, dpi = 300)



#FIGURE 5 DATA
figure5 <- read.dta("populism.dta")

#FIGURE 5 COLORS
cols <- c("#72D8FF", "#93C47D", "#F76D5E","#FFFFBF")

#FIGURE 5 GRAPH PARTY
poparty <- ggplot(subset(figure5, !is.na(party)), aes(x=AKK_CFA, fill = party)) + 
  geom_density(alpha = 0.5) + 
  theme_bw() + theme(panel.grid = element_blank()) +
  theme(axis.text=element_text(size=10)) +
  xlab("Populist attitudes") +
  scale_fill_manual(values = cols) +
  theme(legend.position = "bottom")

#FIGURE 5 GRAPH LEVEL
poplevel <- ggplot(figure5, aes(x=AKK_CFA, fill = level)) + 
  geom_density(alpha = 0.5) + 
  theme_bw() + theme(panel.grid = element_blank()) +
  theme(axis.text=element_text(size=10)) +
  xlab("Populist attitudes") +
  scale_y_continuous(limits=c(0, 3), breaks=seq(0, 3, by=1)) +
  scale_fill_manual(values = cols) +
  theme(legend.position = "bottom")

ggarrange(poparty, poplevel, nrow=2)
ggsave("figure5.png", width = 8, height = 6, dpi = 300)



# FIGURE A1 DATA
figure <- read_dta("breakoffs.dta")

# FIGURE A1 GRAPH
ggplot(data=figure) + 
  geom_histogram(aes(x=question, fill = question), colour = 2, fill = "white",
                 bins = 63) +
  theme_bw() + 
  theme(panel.grid = element_blank()) +
  theme(axis.text=element_text(size=8)) +
  ylab("N of dropouts during the interview") +
  xlab("Question number") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_x_continuous(limits=c(0, 64), breaks=seq(1, 63, by=1)) +
  annotate(geom = "text", x = 1, size = 2.5,
           y = 135, label = "Taxes vs Services",
           color = "gray20"[1], vjust = -0.5) +
  annotate(geom = "text", x = 8, size = 2.5,
           y = 70, label = "Aspects of decision",
           color = "gray20"[1], vjust = -0.5) +
  annotate(geom = "text", x = 13, size = 2.5,
           y = 50, label = "Exp. pub. opinion",
           color = "gray20"[1], vjust = -0.5) + 
  annotate(geom = "text", x = 24, size = 2.5,
           y = 25, label = "Populism scale",
           color = "gray20"[1], vjust = -0.5) +
  annotate(geom = "text", x = 21, size = 2.5,
           y = 35, label = "Congruence: Taxes vs Services",
           color = "gray20"[1], vjust = -0.5)

ggsave("figureA1.png", width = 9, height = 5, dpi = 300)
